import Vue from 'vue'
import Vuex from 'vuex'
import { getBills, getCategories, setBills, setCategories } from '@/assets/utils'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    bills: getBills(), // 没有格式化的账单数据，展示的时候需要进行格式化
    categories: getCategories(),
    incomeAmount: 0,
    expendAmount: 0
  },
  mutations: {
    setBills (state, { bills }) {
      setBills(bills)
      state.bills = bills
    },
    setCategories (state, { categories }) {
      setCategories(categories)
      state.categories = categories
    },
    setIncomeAmount (state, { incomeAmount }) {
      state.incomeAmount = incomeAmount
    },
    setExpendAmount (state, { expendAmount }) {
      state.expendAmount = expendAmount
    }
  },
  actions: {
    addBills ({ state, commit }, { bills }) {
      commit('setBills', {
        bills: state.bills.concat(bills)
      })
    }
  },
  modules: {
  }
})
